﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AMail.Common.Interfaces.Data;
using AMail.Common;

namespace AMail.Modules.Data.Actions
{
  public class AddFoldersAction : IMailAction
  {
    private List<IMailFolder> folders;

    public AddFoldersAction(IEnumerable<IMailFolder> folders)
    {
      this.folders = new List<IMailFolder>(folders);
    }

    public AddFoldersAction(IMailFolder folder)
    {
      this.folders = new List<IMailFolder> { folder };
    }

    #region IMailAction Members

    public void Commit(IMailArchiver archiver)
    {
      var newMessages = new List<IMailMessage>();
      foreach (var folder in this.folders)
        folder.FindMessages(m => m.IsNew, newMessages);

      foreach (var message in newMessages)
        archiver.AddMessage(message);
    }

    public void Undo()
    {
      foreach (var folder in this.folders)
        folder.ParentFolder = null;
    }

    #endregion

    public override string ToString()
    {
      if (this.folders.Count == 1)
        return string.Format("Added folder '{0}' to '{1}'", this.folders[0].Name, this.folders[0].ParentFolder.Name);
      else
        return string.Format("Added {0} folders to '{1}'", this.folders.Count, this.folders[0].ParentFolder.Name);
    }
  }
}
